package com.shijiu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijiu.dao.mapper.SignMapper;
import com.shijiu.pojo.Sign;
import com.shijiu.service.SignService;
import com.shijiu.common.Result;
import com.shijiu.vo.SighListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author xlj
 * @Date Created in  2023/4/20 9:54
 * @DESCRIPTION:
 * @Version V1.0
 */

@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements SignService {

    @Resource
    private SignMapper signMapper;

    private static final Integer INTEGRAL = 20;

    private static final String IS_DELETED = "0";
    @Override
    public Result saveOrUpdate(Integer shopId, Sign sign) {
        Optional.ofNullable(shopId).orElseThrow(() -> new RuntimeException("shopId is null"));
        //获取当前时间
        String today = DateUtil.today();
        Date dateTime = DateUtil.parse(today, "yyyy-MM-dd");
        LambdaQueryWrapper<Sign> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sign::getShopId,shopId);
        Sign selectOne = signMapper.selectOne(wrapper);
        Sign signMessage = new Sign();
        BeanUtil.copyProperties(sign, signMessage);
        //一次加20积分,可以低效xxx钱，暂时还在思考中；
        signMessage.setIntegral(INTEGRAL);
        Integer count = 0;
        if (selectOne != null) {
            //获取上次的打卡时间
            Date dakaTime = selectOne.getDakaTime();
            long days = DateUtil.betweenDay(dateTime, dakaTime, true);
            if(days < 1) {
                // 时间间距小于1天
                return Result.fail(Integer.parseInt("500"),"不能一天打卡2次");
            } else {
                // 时间间距大于1天
                Integer clockingRecords = selectOne.getClockingRecords();
                clockingRecords = clockingRecords+1;
                Integer integral = selectOne.getIntegral();
                integral = integral+20;
                String comments = selectOne.getComments();
                signMessage.setComments(comments);
                signMessage.setIntegral(integral);
                signMessage.setClockingRecords(clockingRecords);
                signMessage.setDakaTime(dateTime);
                signMessage.setId(selectOne.getId());
                signMessage.setShopId(shopId);
                signMessage.setUpdateTime(dateTime);
                //走更新 根据商家ID进行修改
                count = signMapper.updateById(signMessage);
                boolean b = count > 0 ? true : false;
                return Result.success(b);
            }
        }
        //走新增
        signMessage.setShopId(shopId);
        signMessage.setClockingRecords(1);
        signMessage.setIsdeleted(IS_DELETED);
        signMessage.setDakaTime(dateTime);
        signMessage.setUpdateTime(dateTime);
        count = signMapper.insert(signMessage);
        boolean b = count > 0 ? true : false;
        return Result.success(b);
    }

    @Override
    public Result selectOrderListByPage(Map<String, Object> requestBodyMaps) {
        Integer pageCount = (Integer) requestBodyMaps.get("page");
        Integer pageSize = (Integer) requestBodyMaps.get("pageSize");
        String username = (String) requestBodyMaps.get("username");
        String location = (String) requestBodyMaps.get("location");
        String comments = (String) requestBodyMaps.get("comments");
        LambdaQueryWrapper<Sign> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果姓名不为 null 不为空，模糊查询
        if (StrUtil.isNotBlank(username)) {
            lambdaQueryWrapper.like(Sign::getUsername, "%" + username + "%");
        }
        //如果定位不为空，模糊查询定位信息
        if (StrUtil.isNotBlank(location)) {
            lambdaQueryWrapper.like(Sign::getLocation, "%" + location + "%");
        }
        //如果评论不为空，查询评论信息
        if (StrUtil.isNotBlank(comments)) {
            lambdaQueryWrapper.eq(Sign::getComments,  comments);
        }
        lambdaQueryWrapper.eq(Sign::getIsdeleted,"0");
        signMapper.selectList(lambdaQueryWrapper);
        // 查询第pageCount页，每页pageSize条记录
        Page<Sign> page = new Page<>(pageCount, pageSize);
        IPage<Sign> sighPage = signMapper.selectPage(page, lambdaQueryWrapper);
        // 当前页的记录列表
        List<Sign> sighList = sighPage.getRecords();
        // 总记录数
        long total = sighPage.getTotal();
        SighListVo sighListVo = new SighListVo();
        sighListVo.setSighVoList(sighList);
        sighListVo.setTotal(total);
        return Result.success(sighListVo);
    }
}
